package com.newlink.building.main.mvp.presenter

import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.utils.NLog
import com.newlink.building.common_base.model.bean.Base_MqttConnectWrapper
import com.newlink.building.main.mvp.contract.Module_Main_MainContract
import com.newlink.building.main.mvp.model.Module_Main_MainModel
import com.newlink.building.main.other.Module_Main_MainConstants
import com.newlink.building.main.service.MqttV5Service

/**
 * Created by xiaodong on 2022/7/25.
 */
class Module_Main_MainPresenter : BasePresenter<Module_Main_MainContract.Model, Module_Main_MainContract.View>(),
    Module_Main_MainContract.Presenter {

    private val TAG: String = "MainPresenter"

    override fun createModel(): Module_Main_MainContract.Model = Module_Main_MainModel()

    override fun connectMqttServer(context: Context, mqttObj: Base_MqttConnectWrapper) {
        Log.e("fvl_dbg", "[jake] --try to connect mqtt--")
        startService(context, mqttObj)
    }

    private fun startService(context: Context, mqttObj: Base_MqttConnectWrapper) {
//        val alarmManager = context.getSystemService(Service.ALARM_SERVICE) as AlarmManager
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
//            val hasPermission = alarmManager.canScheduleExactAlarms()
//            if (!hasPermission) {
//                Log.e("fvl_dbg", "no permission")
//                val uri = Uri.parse("package:" + context.packageName)
//                val i = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, uri)
//                context.startActivity(i)
//                return
//            } else {
//                startMqttService(context, mqttObj)
//            }
//        } else {
//            startMqttService(context, mqttObj)
//        }
        startMqttService(context, mqttObj)
    }

    private fun startMqttService(
        context: Context,
        mqttObj: Base_MqttConnectWrapper
    ) {
        Log.e(TAG, "[jake]  操作 MqttV5Service from ${javaClass.simpleName} <startMqttService>")
        val i = Intent(context, MqttV5Service::class.java).apply {
            action = Module_Main_MainConstants.COMMAND_CONNECT_MQTT
            putExtra(Module_Main_MainConstants.COMMAND_MQTT_OBJ_BUNDLE, mqttObj)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(i)
        } else {
            context.startService(i)
        }
    }

    override fun logout(token: String, reason: Int) {
        mModel?.logout(token)?.ss(mModel, mView) { logoutData ->
            NLog.e("logout response ->  ${logoutData?.code}")
            mView?.showLogoutSuccess(logoutData?.code == 200, reason)
        }
    }

    override fun getUserInfo(token: String, userId: Long) {
        mModel?.getUserInfo(token, userId)?.ss(mModel, mView) { infoData ->
            NLog.e("getUserInfo response ->  ${infoData?.data}")
            if (infoData?.data == null) {
            } else {
                mView?.showUserInfo(infoData?.data!!)
            }
        }
    }

    override fun checkTokenAvailable(token: String) {
        mModel?.checkTokenAvailable(token)?.ss(mModel, mView) {
            mView?.tokenAvailable()
        }
    }
}
